package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @since 2022/08/25 09:09
 * @author sunxuJ
 */
@Service
public class UserLoginServiceImpl implements ILoginService {

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserConverterMapper userConverterMapper;


    @Override
    public UserLoginResponse login(UserLoginRequest request, HttpServletResponse httpServletResponse) {

        String password = DigestUtils.md5DigestAsHex(request.getUserPwd().getBytes());

        //先判断是否用户名或密码不正确

        Example example2 = new Example(Member.class);
        example2.createCriteria().andEqualTo("username",request.getUserName())
                .andEqualTo("password",password);
        List<Member> members2 = memberMapper.selectByExample(example2);
        if(members2.size() == 0){
            throw new BizException(UserRetCode.USERORPASSWORD_ERRROR.getCode(),UserRetCode.USERORPASSWORD_ERRROR.getMessage());
        }

        //判断是否是未激活问题
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username",request.getUserName())
                .andEqualTo("password",password)
                .andEqualTo("isVerified","Y");
        List<Member> members = memberMapper.selectByExample(example);
        if(members.size() == 0){
            throw new BizException(UserRetCode.USER_ISVERFIED_ERROR.getCode(),UserRetCode.USER_ISVERFIED_ERROR.getMessage());
        }



        UserLoginResponse loginResponse = userConverterMapper.memberToUserLoginResponse(members.get(0));

        //设置token (uid和username)
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("uid",members.get(0).getId());
        hashMap.put("username",members.get(0).getUsername());
        hashMap.put("file",members.get(0).getFile());
        String tokenValue = JSON.toJSONString(hashMap);
        JwtTokenUtils createIwtTool = JwtTokenUtils.builder().msg(tokenValue).build();
        String token = createIwtTool.creatJwtToken();
        loginResponse.setToken(token);

        //设置cookie
        Cookie cookie = new Cookie("access_token",token);
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
        return loginResponse;
    }
}
